package com.g1.controller;

import com.g1.entity.Student;
import com.g1.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("selectAll")
    public ModelAndView selectAll(){
        ModelAndView modelAndView=new ModelAndView("studentList");
        List<Student> students = studentService.selectAll();
        modelAndView.addObject("students",students);
        return modelAndView;
    }

    @RequestMapping("selectById")
    public ModelAndView  selectById(@RequestParam("id") Long id){
        ModelAndView modelAndView=new ModelAndView("updateStudent");
        Student student = studentService.selectById(id);
        modelAndView.addObject("student",student);
        return modelAndView;
    }

    @RequestMapping("update")
    public String update(Student stu){
        int update = studentService.update(stu);
        return "redirect:/studentList";
    };

    @RequestMapping("delete")
    public String delete(Long id){
        int delete = studentService.delete(id);
        return "redirect:/studentList";
    };

    @RequestMapping("insert")
    public String insert(Student student){
        int insert = studentService.insert(student);
        return "redirect:/studentList";
    };
}
